<?php
namespace Home\Controller;
use Think\Controller;
class WeixinController extends  PublicController
{
   
    /**
     * 菜单设置
     */
    public function index(){
    	$list=M("caidan")->order('fid asc,sort asc')->select();
      
        foreach ( $list as $k => $vo ) {
			if ($vo ['fid'] != 0)
				continue;
			
			$one_arr [$vo ['id']] = $vo;
			unset ( $list [$k] );
		}
	
		foreach ( $one_arr as $p ) {
			$data [] = $p;
			
			$two_arr = array ();
			foreach ( $list as $key => $l ) {
				if ($l ['fid'] != $p ['id'])
					continue;
				
				$l ['cname'] = '├──' . $l ['cname'];
				$two_arr [] = $l;
				unset ( $list [$key] );
			}
			
			$data = array_merge ( $data, $two_arr );
		}	
		
    	$this->assign("list",$data);
    	$this->display();

    }
    
    /**
     * 微信参数设置
     */
    public function set(){
    	if(I('get.id')){
    		$list = M('weixin')->where(array('id'=>I('get.id')))->find();
    		$this->assign('list',$list);
    	}
    	$this->display();
    }
    
    public function weixin(){
        $list=M("weixin")->select();
        if(is_array($list)){
            $this->assign("list",$list);
            $this->display();
        }else{
            $this->error("请设置公众账号");
        }
    }
    /**
     * 微信设置公众号
     */
    public function setaccount(){
        $data["gongzhonghao"]=I("post.gongzhonghao");
        $data["gongzhonghaooldid"]=I("post.yuanshihao");
        $data["weixinid"]=I("post.weixinid");
        $data["appid"]=I("post.appid");
        $data["secret"]=I("post.secret");
        $flag=true;
        foreach ($data as $value){
            if(empty($value)){
                $flag=false;
                break;
            }
        }
        if($flag===false){
            $this->error("参数不能为空！");
        }else{
        	if(I('post.id')){
        		if(M("weixin")->where(array('id'=>I('post.id')))->data($data)->save()){
        			$this->success("修改成功",U("Weixin/weixin"));
        		}else{
        			$this->error("修改失败");
        		}
        	}else{
	            $data["times"]=date("Y-m-d H:i:s",time());
	            if(M("weixin")->data($data)->add()){
	                $this->success("添加成功",U("Weixin/weixin"));
	            }else{
	                $this->error("添加失败");
	            }
        	}
        }
    }
    
    
    public function del(){
    	$id=I('get.id');
        if(M("weixin")->where(array('id'=>I('get.id')))->delete()){
        			$this->success("删除成功",U("Weixin/weixin"));
        		}else{
        			$this->error("删除失败");
        		}
    }
    
    /**
     * 微信菜单设置
     */
    public function cai(){
    	$where['fid']=array(0,neq);
    	$ca = M('caidan')->where($where)->select();
    	if(I('get.id')){
    		$list = M('caidan')->where(array('id'=>I('get.id')))->find();
    		if($list['fid'] != 0){
    			$ll = M('caidan')->where(array('id'=>$list['fid']))->find();
    		}
    		$this->assign('list',$list);
    		$this->assign('ll',$ll);
    	}
    	
    	$this->assign('ca',$ca);
    	$this->display();
    }
    
    /**
     * 微信菜单设置提交
     */
    public function setca(){
    	$data["sort"]=I("post.sort");
    	$data["fid"]=I("post.fid");
    	$data["cname"]=I("post.cname");
    	$data["relation"]=I("post.relation");
    	$data["url"]=I("post.url");
    	if(M('caidan')->where(array('cname'=>I("post.cname")))->find()){
    		$this->error("菜单名称不能相同！",U("Weixin/index"));
    	}
    	if(I('post.id')){
    			if(M("caidan")->where(array('id'=>I('post.id')))->data($data)->save()){
    				$this->success("修改成功",U("Weixin/index"));
    			}else{
    				$this->error("修改失败");
    			}
    		}else{
    			
    			if(M("caidan")->data($data)->add()){
    				$this->success("添加成功",U("Weixin/index"));
    			}else{
    				$this->error("添加失败");
    			}
    		}
    	
    }
    
    public function delcai(){
    	$id=I('get.id');
    	if($id){
	    	if(M("caidan")->where(array('id'=>I('get.id')))->delete()){
	    		$this->success("删除成功",U("Weixin/index"));
	    	}else{
	    		$this->error("删除失败");
	    	}
    	}
    }
    
    //批量删除
    public function pdel(){
    	
    	$che['id'] = array('in',$_POST['check']);
    	if(M("caidan")->where($che)->delete()){
    		$this->success("删除成功",U("Weixin/index"));
    	}else{
    		$this->error("删除失败");
    	}
    }
    
}


?>